package com.tianqu.tms.www.transfer.controller;
/**
 * 用于将本地图片上传到服务器上的工具类
 * 
 * 将本地图片重命名定位
 * 
 * 上传到服务器并更新到数据库中 
 */
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.CopyFile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;

import jcifs.smb.SmbFile;

public class TransferRgAdvanceInputConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(TransferRgAdvanceInputConfirmController.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			String serial = (String) uc.getAttribute("serial");
			String sysPath = (String) uc.getAttribute("sysPath");
			String localIp = (String)uc.getAttribute("localIp");
			String picPoint = (String)uc.getAttribute("picPoint");
			uc.removeAttribute("serial");
			uc.removeAttribute("sysPath");
			uc.removeAttribute("localIp");
			uc.removeAttribute("picPoint");
			if (serial != null && !"".equals(serial)) {
			// 开始读取
			//String path = "smb://" + "192.168.2.79" + "/1/";
			//String path = "smb://192.168.2.79/1/";
			String path = "smb://" + localIp+ "/1/";
			SmbFile file = new SmbFile(path);
			if (!file.exists()) {
				logger.error("path不存在.");
				controller.commit();
				return;
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				String carInfoID = "";
				TransferAcceptedInfo accept = new TransferAcceptedInfo();
				accept = accept.findTransStatusBySerial(serial);
				String sortTime ="";
				List list = new ArrayList();
				for (int i = 0; i < filelist.length; i++) {
					SmbFile readfile = new SmbFile(path + "" + filelist[i]);
					if (!readfile.isDirectory()) {
						long a = readfile.lastModified();
						sortTime += String.valueOf(a)+",";
						String name[] = filelist[i].split("=");
						carInfoID = name[0];
						Map map = new HashMap();
						map.put(String.valueOf(a), carInfoID);
						list.add(map);
					} 
				}
				File outFile = null;
				SmbFile inFile = null;
				List realList = new ArrayList();
				if(sortTime.length() > 0)
				{
					String[] bb = sortTime.split(",");
					Arrays.sort(bb); 
					List yyList = Arrays.asList(bb);	
					for(int i = 0;i<yyList.size();i++)
					{
						String sunxu = (String)yyList.get(i);
						for(int j=0;j<list.size();j++)
						{
							Map nameMap = (Map)list.get(j);
							String nameVa = (String)nameMap.get(sunxu);
							if(nameVa!=null)
							{
								Map realMap = new HashMap();
								realMap.put(i, nameVa);
								realList.add(realMap);
							}
						}
					}
					if (realList != null && realList.size() > 0) {
						for (int p = 0; p < realList.size(); p++) {
							Map map = (Map) realList.get(p);
							String cid = (String) map.get(p);
							if(cid.indexOf("jpg")!=-1)
							{
								if(picPoint!=null && !"".equals(picPoint))
								{
									if("1".equals(picPoint))
									{
										accept.setMaterialImages1(imgPath(serial,"1"));
									}
									if("2".equals(picPoint))
									{
										accept.setMaterialImages2(imgPath(serial,"2"));
									}
									if("3".equals(picPoint))
									{
										accept.setMaterialImages3(imgPath(serial,"3"));
									}
									if("4".equals(picPoint))
									{
										accept.setMaterialImages4(imgPath(serial,"4"));
									}
									if("5".equals(picPoint))
									{
										accept.setMaterialImages5(imgPath(serial,"5"));
									}	
									if("6".equals(picPoint))
									{
										accept.setOwnersGroupPhoto(imgPath(serial,"6"));
									}									
									// 对每个path进行处理
									inFile = new SmbFile(path + "" + cid);
	 
									outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + serial + "/") + serial + "_"+ (picPoint) + ".jpg");
									CopyFile.copy2(inFile, outFile);
									inFile.delete();// 添加完之后直接删除
								}else{
									if (p == 0) {
										accept.setMaterialImages1(imgPath(serial,String.valueOf(p + 1)));
									}
									if (p == 1) {
										accept.setMaterialImages2(imgPath(serial,String.valueOf(p + 1)));
									}
									if (p == 2) {
										accept.setMaterialImages3(imgPath(serial,String.valueOf(p + 1)));
									}
									if (p == 3) {
										accept.setMaterialImages4(imgPath(serial,String.valueOf(p + 1)));
									}
									if (p == 4) {
										accept.setMaterialImages5(imgPath(serial,String.valueOf(p + 1)));
									}
									
									// 对每个path进行处理
									inFile = new SmbFile(path + "" + cid);
									outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + "/"+ serial + "/") + serial + "_"+ (p + 1) + ".jpg");
									CopyFile.copy2(inFile, outFile);
									inFile.delete();// 添加完之后直接删除
								}
							}
						}
					}
				}
				//开始更新
				accept.setSerial(serial);
				accept.updatePic(accept);
			  }
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String serial,String imgKey){
		return ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + serial+"/"+serial+"_"+imgKey+".jpg");	
	}
 
}
